package com.dmap.admin.service.backgroundv1.impl;

import com.dmap.admin.bo.user.UserInfo;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.dao.AdminPermissionsMapper;
import com.dmap.admin.dao.AdminRolePermissionsMapper;
import com.dmap.admin.po.*;
import com.dmap.admin.service.backgroundv1.RoleService;
import com.dmap.admin.service.backgroundv1.UserPermissionManageService;
import com.dmap.admin.service.backgroundv1.UserService;
import com.dmap.admin.vo.PermissionVo;
import com.dmap.admin.vo.UserPermissionVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 10:46
 * \* Description:
 * \
 */
@Service
public class UserPermissionManageServiceImpl implements UserPermissionManageService {

    @Resource
    private AdminRolePermissionsMapper adminRolePermissionsMapper;
    @Resource
    private AdminPermissionsMapper adminPermissionsMapper;
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;

    @Override
    public UserPermissionVo selectUserPermissionsByUserId(Long id) {
        AdminUser adminUser = userService.selectUserById(id);
        if (adminUser == null) {
            return null;
        }
        List<PermissionVo> permissionVoList = this.selectPermissionVoListByRoleId(adminUser.getRoleId());
        this.sortStringMethod(permissionVoList);
        UserInfo userInfo = new UserInfo(adminUser);
        UserPermissionVo userPermissionVo = new UserPermissionVo();
        userPermissionVo.setUserInfo(userInfo);
        userPermissionVo.setPermissionVoList(permissionVoList);
        return userPermissionVo;
    }

    private List<PermissionVo> selectPermissionVoListByRoleId(Long roleId) {
        AdminRole adminRole = roleService.selectRoleById(roleId);
        if (adminRole == null) {
            return null;
        }
        //超级管理员 has all permissions
        List<AdminPermissions> adminPermissionsList = new ArrayList<>();
        if (adminRole.getName().equals(AdminConstant.SUPER_ADMIN_ROLE)) {
            adminPermissionsList = adminPermissionsMapper.selectByExample(null);
        } else {
            AdminRolePermissionsExample adminRolePermissionsExample = new AdminRolePermissionsExample();
            adminRolePermissionsExample.createCriteria().andRoleIdEqualTo(roleId);
            List<AdminRolePermissions> adminRolePermissionsList = adminRolePermissionsMapper.selectByExample(adminRolePermissionsExample);
            if (CollectionUtils.isEmpty(adminRolePermissionsList)) {
                return null;
            }
            List<Long> permissionsIdList = new ArrayList<>();
            for (AdminRolePermissions adminRolePermissions : adminRolePermissionsList) {
                permissionsIdList.add(adminRolePermissions.getPermissionsId());
            }
            AdminPermissionsExample adminPermissionsExample = new AdminPermissionsExample();
            adminPermissionsExample.createCriteria().andIdIn(permissionsIdList);
            adminPermissionsList = adminPermissionsMapper.selectByExample(adminPermissionsExample);
        }
        if (CollectionUtils.isEmpty(adminPermissionsList)) {
            return null;
        }
        List<PermissionVo> permissionVoList = new ArrayList<>();
        Set<AdminPermissions> hasChildSet = new HashSet<>();
        //把是爹的找出来
        for (AdminPermissions permission01 : adminPermissionsList) {
            if (permission01.getHasChild() == AdminConstant.HAS_CHILD) {
                hasChildSet.add(permission01);
                for (AdminPermissions permission03 : adminPermissionsList) {
                    if (permission01.getParentId().toString().equals(permission03.getId().toString())) {
                        hasChildSet.remove(permission01);
                    }
                }
            }
        }
        for (AdminPermissions permission02 : hasChildSet) {
            PermissionVo permissionVo02 = new PermissionVo(permission02);
            Set<AdminPermissions> hasChildSet02 = new HashSet<>();
            List<PermissionVo> permissionVoList02 = new ArrayList<>();
            for (AdminPermissions p2 : adminPermissionsList) {
                if (permission02.getId().toString().equals(p2.getParentId().toString())) {
                    hasChildSet02.add(p2);
                }
            }
            for (AdminPermissions permission04 : hasChildSet02) {
                PermissionVo permissionVo04 = new PermissionVo(permission04);
                Set<AdminPermissions> hasChildSet04 = new HashSet<>();
                List<PermissionVo> permissionVoList04 = new ArrayList<>();
                for (AdminPermissions p2 : adminPermissionsList) {
                    if (permission04.getId().toString().equals(p2.getParentId().toString())) {
                        hasChildSet04.add(p2);
                    }
                }
                for (AdminPermissions permission06 : hasChildSet04) {
                    PermissionVo permissionVo06 = new PermissionVo(permission06);
                    Set<AdminPermissions> hasChildSet06 = new HashSet<>();
                    List<PermissionVo> permissionVoList06 = new ArrayList<>();
                    for (AdminPermissions p2 : adminPermissionsList) {
                        if (permission06.getId().toString().equals(p2.getParentId().toString())) {
                            hasChildSet06.add(p2);
                        }
                    }
                    for (AdminPermissions permission08 : hasChildSet06) {
                        PermissionVo permissionVo08 = new PermissionVo(permission08);
                        Set<AdminPermissions> hasChildSet08 = new HashSet<>();
                        List<PermissionVo> permissionVoList08 = new ArrayList<>();
                        for (AdminPermissions p2 : adminPermissionsList) {
                            if (permission08.getId().toString().equals(p2.getParentId().toString())) {
                                hasChildSet08.add(p2);
                            }
                        }
                        for (AdminPermissions permission10 : hasChildSet08) {
                            PermissionVo permissionVo10 = new PermissionVo(permission10);
                            Set<AdminPermissions> hasChildSet10 = new HashSet<>();
                            List<PermissionVo> permissionVoList10 = new ArrayList<>();
                            for (AdminPermissions p2 : adminPermissionsList) {
                                if (permission10.getId().toString().equals(p2.getParentId().toString())) {
                                    hasChildSet10.add(p2);
                                }
                            }
                            for (AdminPermissions permission12 : hasChildSet10) {
                                PermissionVo permissionVo12 = new PermissionVo(permission12);
                                Set<AdminPermissions> hasChildSet12 = new HashSet<>();
                                List<PermissionVo> permissionVoList12 = new ArrayList<>();
                                for (AdminPermissions p2 : adminPermissionsList) {
                                    if (permission12.getId().toString().equals(p2.getParentId().toString())) {
                                        hasChildSet12.add(p2);
                                    }
                                }
                                this.sortStringMethod(permissionVoList12);
                                permissionVo12.setChildPermissionVoList(permissionVoList12);
                                permissionVoList10.add(permissionVo12);
                            }
                            this.sortStringMethod(permissionVoList10);
                            permissionVo10.setChildPermissionVoList(permissionVoList10);
                            permissionVoList08.add(permissionVo10);

                        }
                        this.sortStringMethod(permissionVoList08);
                        permissionVo08.setChildPermissionVoList(permissionVoList08);
                        permissionVoList06.add(permissionVo08);

                    }
                    this.sortStringMethod(permissionVoList06);
                    permissionVo06.setChildPermissionVoList(permissionVoList06);
                    permissionVoList04.add(permissionVo06);
                }
                this.sortStringMethod(permissionVoList04);
                permissionVo04.setChildPermissionVoList(permissionVoList04);
                permissionVoList02.add(permissionVo04);
            }
            this.sortStringMethod(permissionVoList02);
            permissionVo02.setChildPermissionVoList(permissionVoList02);
            permissionVoList.add(permissionVo02);
        }
        return permissionVoList;
    }

    private void sortStringMethod(List<PermissionVo> permissionVoList) {
        Collections.sort(permissionVoList, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                PermissionVo permissionVo01 = (PermissionVo) o1;
                PermissionVo permissionVo02 = (PermissionVo) o2;
                return permissionVo01.getName().compareTo(permissionVo02.getName());
            }
        });
    }

}